<?php
session_start();

// 检查用户是否已登录
if (!isset($_SESSION['username'])) {
    header('Location: login.php');
    exit();
}
require_once 'track_online.php';
// 当前用户信息
$currentUser = $_SESSION['username'];
$userDir = __DIR__ . "/user/$currentUser";
$infoFile = "$userDir/info.json";

// 初始化消息提示
$message = '';

// 处理退出登录
if (isset($_POST['logout_confirmed'])) {
    session_destroy();
    header('Location: login.php');
    exit();
}

// 处理修改密码
if (isset($_POST['change_password'])) {
    $newPassword = $_POST['new_password'] ?? '';
    $confirmPassword = $_POST['confirm_password'] ?? '';

    if ($newPassword === $confirmPassword && !empty($newPassword)) {
        $userInfo = json_decode(file_get_contents($infoFile), true);
        $userInfo['password'] = password_hash($newPassword, PASSWORD_DEFAULT);
        file_put_contents($infoFile, json_encode($userInfo));
        $message = '密码已成功修改';
    } else {
        $message = '两次密码输入不一致或密码为空';
    }
}

// 处理注销账户
if (isset($_POST['delete_account_confirmed'])) {
    if (is_dir($userDir)) {
        array_map('unlink', glob("$userDir/*")); // 删除文件夹内的所有文件
        rmdir($userDir); // 删除文件夹
    }
    session_destroy();
    header('Location: register.php'); // 跳转到注册页面
    exit();
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>账户设置 - JIILCHAT</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
        }
        
        body {
            background-color: #f5f7fa;
            color: #333;
        }
        
        /* 顶部导航栏 */
        .navbar {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            background: #ffffff;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            z-index: 100;
            border-bottom: 1px solid #e0e4e9;
        }
        
        .back-btn {
            background: none;
            border: none;
            color: #000;
            font-size: 20px;
            margin-right: 15px;
            cursor: pointer;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            background: #e4e6eb;
        }
        
        .navbar-title {
            font-size: 18px;
            font-weight: 600;
            flex-grow: 1;
            color: #333;
        }
        
        /* 主内容区 */
        .content {
            margin-top: 70px;
            padding: 20px;
        }
        
        .settings-card {
            background: white;
            border-radius: 16px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            margin-bottom: 25px;
        }
        
        .card-title {
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 25px;
            padding-bottom: 15px;
            border-bottom: 1px solid #f0f2f5;
            color: #222;
            display: flex;
            align-items: center;
        }
        
        .card-title i {
            margin-right: 12px;
            color: #3498db;
            font-size: 24px;
        }
        
        /* 消息提示 */
        .message {
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 12px;
            font-size: 15px;
            display: flex;
            align-items: center;
        }
        
        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .message i {
            margin-right: 10px;
            font-size: 20px;
        }
        
        /* 表单样式 */
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #444;
            font-size: 15px;
        }
        
        .form-input {
            width: 100%;
            padding: 14px;
            border: 1px solid #e0e4e9;
            border-radius: 12px;
            font-size: 15px;
            transition: all 0.3s ease;
        }
        
        .form-input:focus {
            border-color: #3498db;
            outline: none;
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
        }
        
        .action-btn {
            background: #3498db;
            color: white;
            border: none;
            border-radius: 12px;
            padding: 15px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            width: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-bottom: 15px;
        }
        
        .action-btn i {
            margin-right: 10px;
        }
        
        .action-btn:hover {
            background: #2980b9;
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(52, 152, 219, 0.3);
        }
        
        .logout-btn {
            background: #e74c3c;
        }
        
        .logout-btn:hover {
            background: #c0392b;
            box-shadow: 0 4px 10px rgba(231, 76, 60, 0.3);
        }
        
        .delete-btn {
            background: #95a5a6;
        }
        
        .delete-btn:hover {
            background: #7f8c8d;
            box-shadow: 0 4px 10px rgba(149, 165, 166, 0.3);
        }
        
        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 200;
            justify-content: center;
            align-items: center;
        }
        
        .modal.active {
            display: flex;
        }
        
        .modal-content {
            background: white;
            width: 90%;
            max-width: 400px;
            border-radius: 16px;
            padding: 25px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.2);
            text-align: center;
        }
        
        .modal-icon {
            font-size: 48px;
            margin-bottom: 20px;
        }
        
        .logout-modal .modal-icon {
            color: #3498db;
        }
        
        .delete-modal .modal-icon {
            color: #e74c3c;
        }
        
        .modal-title {
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 15px;
            color: #222;
        }
        
        .modal-text {
            font-size: 16px;
            color: #555;
            margin-bottom: 25px;
            line-height: 1.5;
        }
        
        .modal-buttons {
            display: flex;
            gap: 15px;
            justify-content: center;
        }
        
        .modal-btn {
            padding: 12px 25px;
            border: none;
            border-radius: 12px;
            font-size: 15px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            min-width: 100px;
        }
        
        .modal-btn.confirm {
            background: #3498db;
            color: white;
        }
        
        .modal-btn.confirm:hover {
            background: #2980b9;
            transform: translateY(-2px);
        }
        
        .modal-btn.cancel {
            background: #ecf0f1;
            color: #333;
        }
        
        .modal-btn.cancel:hover {
            background: #d0d3d4;
            transform: translateY(-2px);
        }
        
        .delete-modal .modal-btn.confirm {
            background: #e74c3c;
        }
        
        .delete-modal .modal-btn.confirm:hover {
            background: #c0392b;
        }
    </style>
    <script>
        function showModal(modalId) {
            document.getElementById(modalId).classList.add('active');
        }
        
        function closeModal(modalId) {
            document.getElementById(modalId).classList.remove('active');
        }
    </script>
</head>
<body>
    <!-- 顶部导航栏 -->
    <div class="navbar">
        <button class="back-btn" onclick="history.back()">
            <i class="fas fa-arrow-left"></i>
        </button>
        <div class="navbar-title">账户设置</div>
    </div>
    
    <div class="content">
        <!-- 显示消息提示 -->
        <?php if ($message): ?>
            <div class="message <?= strpos($message, '成功') !== false ? 'success' : 'error' ?>">
                <i class="fas <?= strpos($message, '成功') !== false ? 'fa-check-circle' : 'fa-exclamation-circle' ?>"></i>
                <?= htmlspecialchars($message) ?>
            </div>
        <?php endif; ?>
        
        <div class="settings-card">
            <h2 class="card-title">
                <i class="fas fa-user-cog"></i>
                账户管理
            </h2>
            
            <!-- 退出登录按钮 -->
            <button class="action-btn logout-btn" onclick="showModal('logout-modal')">
                <i class="fas fa-sign-out-alt"></i> 退出登录
            </button>
            
            <!-- 修改密码表单 -->
            <form method="POST">
                <div class="form-group">
                    <label class="form-label" for="new_password">
                        <i class="fas fa-lock"></i> 新密码
                    </label>
                    <input type="password" id="new_password" name="new_password" 
                           class="form-input" placeholder="请输入新密码" required>
                </div>
                
                <div class="form-group">
                    <label class="form-label" for="confirm_password">
                        <i class="fas fa-lock"></i> 确认新密码
                    </label>
                    <input type="password" id="confirm_password" name="confirm_password" 
                           class="form-input" placeholder="请再次输入新密码" required>
                </div>
                
                <button class="action-btn" type="submit" name="change_password">
                    <i class="fas fa-key"></i> 修改密码
                </button>
            </form>
            
            <!-- 注销账户按钮 -->
            <button class="action-btn delete-btn" onclick="showModal('delete-modal')">
                <i class="fas fa-user-slash"></i> 注销账户
            </button>
        </div>
    </div>
    
    <!-- 退出登录确认弹窗 -->
    <div class="modal logout-modal" id="logout-modal">
        <div class="modal-content">
            <div class="modal-icon">
                <i class="fas fa-sign-out-alt"></i>
            </div>
            <h3 class="modal-title">退出登录</h3>
            <p class="modal-text">确定要退出当前账户吗？您需要重新登录才能继续使用。</p>
            <div class="modal-buttons">
                <form method="POST" style="display: inline;">
                    <button type="submit" name="logout_confirmed" class="modal-btn confirm">确定</button>
                </form>
                <button class="modal-btn cancel" onclick="closeModal('logout-modal')">取消</button>
            </div>
        </div>
    </div>
    
    <!-- 注销账户确认弹窗 -->
    <div class="modal delete-modal" id="delete-modal">
        <div class="modal-content">
            <div class="modal-icon">
                <i class="fas fa-exclamation-triangle"></i>
            </div>
            <h3 class="modal-title">注销账户</h3>
            <p class="modal-text">确定要永久注销您的账户吗？此操作将删除所有数据且不可恢复！</p>
            <div class="modal-buttons">
                <form method="POST" style="display: inline;">
                    <button type="submit" name="delete_account_confirmed" class="modal-btn confirm">确定注销</button>
                </form>
                <button class="modal-btn cancel" onclick="closeModal('delete-modal')">取消</button>
            </div>
        </div>
    </div>
</body>
</html>